﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using final_darskade.Models;
using System.Web.Mvc;
namespace final_darskade.Models.klassModel
{
    /// <summary>
    /// Return Summary information of the Klass
    /// </summary>

    public class klassShow
    {
        
        public string courseName { get; set; }
        public string profFirstName { get; set; }
        public string profLastName { get; set; }
        public int id { get; set; }
        public int time_id { get; set; }
        public int term { get; set; }
        public DateTime start_exam_date_time { get; set; }
        public DateTime end_exam_date_time { get; set; }
        public klassShow()
        {
        }
        public klassShow(string klassCourseName, string klassProfessorName, string klassProfessorLastName, int klassTimeId, int klassTerm, DateTime examBegin, DateTime examEnd)
        {
            this.courseName = klassCourseName;
            // this.profFirstName = klassProfessorName;
            this.profLastName = klassProfessorLastName;
            this.time_id = klassTimeId;
            this.term = klassTerm;
            this.start_exam_date_time = examBegin;
            this.end_exam_date_time = examEnd;

        }

        internal void AddToklassShow(klassShow klassmodel, String last_name, [Bind(Exclude = "course_id,unit")]course cours, time_in_week time)
        {
            // throw new NotImplementedException();
            darskadeEntities de = new darskadeEntities();

            //user use;
            //course cours;
            //time_in_week time;

            last_name = klassmodel.profLastName;
            cours.name = klassmodel.courseName;
            time.id = klassmodel.time_id;
            term = klassmodel.term;
            start_exam_date_time = klassmodel.start_exam_date_time;
            end_exam_date_time = klassmodel.end_exam_date_time;

        }

        //internal void SaveChanges()
        //{
        //    throw new NotImplementedException();

        //

    }

    public interface KlassRepository
    {
        List<klassShow> GetFrontPageSummaries();

        List<course> GetCourseName();

        List<time_in_week> GetTimeid();

        List<user> GetProfessorName();

        klassShow findKlassShow(int klassId);

        klass findKlass(int klassId);

        void updateKlass(klass modifiedKlass);

        void deleteKlass(int klassId);
    }

    public class KlassShowRepository : KlassRepository
    {
        private darskadeEntities de = new darskadeEntities();
        ///<Summary>
        ///Gets the Front Page Listing
        ///</Summary>
        ///<returns> A Listing of Klass </returns>
        public List<klassShow> GetFrontPageSummaries()
        {
            List<klassShow> ksList = new List<klassShow>();
            List<klass> kList =
            (from k in de.klasses
             where k.id == k.id
             select k).ToList();
            //new klassShow(k.course.name, k.user.first_name, k.user.last_name, k.time_id, k.term, k.start_exam_date_time ?? default(DateTime), k.end_exam_date_time ?? default(DateTime))).ToList();
            foreach (klass k in kList)
            {
                klassShow ks = new klassShow();
                ks.id = k.id;
                ks.courseName = k.course.name;
                ks.profFirstName = k.user.first_name;
                ks.profLastName = k.user.last_name;
                ks.time_id = k.time_id;
                ks.term = k.term;
                ks.start_exam_date_time = k.start_exam_date_time ?? default(DateTime);
                ks.end_exam_date_time = k.end_exam_date_time ?? default(DateTime);
                ksList.Add(ks);
            }
            //de.SaveChanges();
            return ksList;
        }

        public List<course> GetCourseName()
        {
            /*List<klassShow> ksList = new List<klassShow>();
            List<course> cList =
            (from c in de.courses
             where c.course_id == c.course_id
             select c).ToList();
            foreach (course c in cList)
            {
                klassShow ks = new klassShow();
                ks.courseName =c.name;
                ksList.Add(ks);
            }
            return ksList;*/

            
            return (from c in de.courses
                 where c.course_id == c.course_id
                 select c).ToList();
        }
          
        public List<time_in_week> GetTimeid()
        {
            return
            (from t in de.time_in_week
             where t.id == t.id
             select t).ToList();
        }

       
        public List<user> GetProfessorName()
        {
            return
            (from u in de.users
             where u.user_id ==u.user_id && u.role.role_name == "professor"
             select u).ToList();
        }

        public klassShow findKlassShow(int klassId)
        {
            klass foundKlass =
            (from k in de.klasses
             where k.id == klassId
             select k).SingleOrDefault();

            klassShow res = new klassShow();
            res.id = foundKlass.id;
            res.id = foundKlass.id;
            res.courseName = foundKlass.course.name;
            res.profFirstName = foundKlass.user.first_name;
            res.profLastName = foundKlass.user.last_name;
            res.time_id = foundKlass.time_id;
            res.term = foundKlass.term;
            res.start_exam_date_time = foundKlass.start_exam_date_time ?? default(DateTime);
            res.end_exam_date_time = foundKlass.end_exam_date_time ?? default(DateTime);

            return res;
        }

        public klass findKlass(int klassId)
        {
            klass foundKlass =
            (from k in de.klasses
             where k.id == klassId
             select k).SingleOrDefault();
            return foundKlass;
        }

        public void updateKlass(klass modifiedKlass)
        {
            klass modifiyingKlass =
            (from k in de.klasses
                where k.id == modifiedKlass.id
                select k).SingleOrDefault();
            modifiyingKlass.course_id = modifiedKlass.course_id;
            modifiyingKlass.term = modifiedKlass.term;
            modifiyingKlass.prof_id = modifiedKlass.prof_id;
            modifiyingKlass.time_id = modifiedKlass.time_id;
            modifiyingKlass.start_exam_date_time = modifiedKlass.start_exam_date_time;
            modifiyingKlass.end_exam_date_time = modifiedKlass.end_exam_date_time;
            de.SaveChanges();
        }
        public void deleteKlass(int klassId)
        {
            klass deletingKlass =
            (from k in de.klasses
             where k.id == klassId
             select k).SingleOrDefault();
            de.klasses.DeleteObject(deletingKlass);
            de.SaveChanges();
        }
    }

}
